package com.my.zl.console.service.autocode.impl;

import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.my.zl.common.dto.ReqDTO;
import com.my.zl.common.dto.advancedQuery.PageQueryDTO;
import com.my.zl.common.enums.resp.GlobalEnum;
import com.my.zl.common.exception.BizException;
import com.my.zl.common.utils.CopyUtil;
import com.my.zl.console.common.constant.AutocodeC;
import com.my.zl.console.dao.autocode.ZlProjectModuleMapper;
import com.my.zl.console.dto.autocode.projectModule.ReqProjectModuleAddDTO;
import com.my.zl.console.dto.autocode.projectModule.ReqProjectModuleDelDTO;
import com.my.zl.console.dto.autocode.projectModule.ReqProjectModuleDetailDTO;
import com.my.zl.console.dto.autocode.projectModule.ReqProjectModuleEditDTO;
import com.my.zl.console.dto.autocode.projectModule.ReqProjectModuleQueryDTO;
import com.my.zl.console.dto.autocode.projectModule.ReqProjectModuleStateDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleAddDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleDelDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleDetailDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleEditDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleQueryDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleStateDTO;
import com.my.zl.console.dto.autocode.projectModule.ResProjectModuleVO2;
import com.my.zl.console.entity.autocode.ZlProjectModule;
import com.my.zl.console.po.autocode.projectModule.ZlProjectModuleVo;
import com.my.zl.console.service.autocode.IProjectModuleService;
import com.my.zl.console.service.base.BaseManager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 项目模块配置
 * @author ZL
 */
@Service
public class ProjectModuleServiceImpl extends BaseManager implements IProjectModuleService {

    @Autowired
    private ZlProjectModuleMapper projectModuleMapper;

    @Override
    public ResProjectModuleQueryDTO queryProjectModuleList(ReqProjectModuleQueryDTO reqInfo,PageQueryDTO page) throws Exception{
        // TODO Auto-generated method stub
        ResProjectModuleQueryDTO resDTO = BeanUtil.copyProperties(page, ResProjectModuleQueryDTO.class);
        if(StringUtils.isNotBlank(reqInfo.getModuleTitle())){
            reqInfo.setModuleTitle("%"+reqInfo.getModuleTitle()+"%");
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        
        pageHandle(page);//处理分页数据
        //当效率不行时自行计数，或完全自行分页，先查询总数，如果为0不用再次查询了；开启计数：true是,false否
        //临近第一条SQL查询语句自动分页,如果未消费时(即后面没有触发SQL查询)则需主动调用pageHp.clear()清理
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlProjectModuleVo> list = projectModuleMapper.selectListPage(paramMap);
        resDTO.setData(CopyUtil.copyList(list, ResProjectModuleVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResProjectModuleDetailDTO queryProjectModuleDetail(ReqProjectModuleDetailDTO reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ResProjectModuleDetailDTO res = new ResProjectModuleDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqInfo);
        ZlProjectModule view = projectModuleMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResProjectModuleAddDTO insertProjectModuleInfo(ReqDTO<ReqProjectModuleAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqProjectModuleAddDTO reqData = reqInfo.getReqData();
        ResProjectModuleAddDTO res = new ResProjectModuleAddDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlProjectModule record = new ZlProjectModule();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setModuleId(IdUtil.fastSimpleUUID());
        record.setDataStatus(AutocodeC.DataStatus.ENABLE.getCode());//状态
        
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("moduleId",record.getModuleId());
        //检查主键是否冲突
        if(projectModuleMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalEnum.DUPLICATE_PRIMARY_KEY);
        }
        
        projectModuleMapper.insertSelective(record);
        
        ZlProjectModule keyInfo = projectModuleMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResProjectModuleEditDTO updateProjectModuleInfo(ReqDTO<ReqProjectModuleEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqProjectModuleEditDTO reqData = reqInfo.getReqData();
        ResProjectModuleEditDTO res = new ResProjectModuleEditDTO();
        Instant time = getSystemTime().getUtcTime();
        
        ZlProjectModule record = new ZlProjectModule();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        
        projectModuleMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("moduleId",record.getModuleId());
        ZlProjectModule keyInfo = projectModuleMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResProjectModuleDelDTO deleteProjectModuleInfo(ReqDTO<ReqProjectModuleDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqProjectModuleDelDTO reqData = reqInfo.getReqData();
        ResProjectModuleDelDTO res = new ResProjectModuleDelDTO();
        Instant time = getSystemTime().getUtcTime();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        projectModuleMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResProjectModuleStateDTO updateProjectModuleState(ReqDTO<ReqProjectModuleStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqProjectModuleStateDTO reqData = reqInfo.getReqData();
        ResProjectModuleStateDTO res = new ResProjectModuleStateDTO();
        Instant time = getSystemTime().getUtcTime();
         
        ZlProjectModule record = new ZlProjectModule();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        projectModuleMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("moduleId",record.getModuleId());
        ZlProjectModule keyInfo = projectModuleMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
}